﻿using SSC.ViewModels;
using System.Windows;
using System.Windows.Controls;
using SSC.Models;

namespace SSC.Views
{
    public partial class MaintenanceWindow : Window
    {
        private MaintenanceViewModel _viewModel;

        public MaintenanceWindow()
        {
            InitializeComponent();
            _viewModel = new MaintenanceViewModel();
            this.DataContext = _viewModel;
        }

        private void BtnAdd_Click(object sender, RoutedEventArgs e)
        {
            var addWin = new wdAddMaintenance
            {
                Owner = this
            };

            if (addWin.ShowDialog() == true)
            {
                var newItem = addWin.Result;

                try
                {
                    _viewModel.AddItem(newItem);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("新增失败：" + ex.Message);
                }
            }
        }

        private void BtnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (_viewModel.SelectedItem == null)
                return;

            if (MessageBox.Show("确定删除该保养项目？", "确认", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                try
                {
                    _viewModel.DeleteItem(_viewModel.SelectedItem);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("删除失败：" + ex.Message);
                }
            }
        }

        private void BtnEdit_Click(object sender, RoutedEventArgs e)
        {
            if (_viewModel.SelectedItem == null)
            {
                MessageBox.Show("请先选择一个要编辑的项目！");
                return;
            }

            var editWin = new wdAddMaintenance(_viewModel.SelectedItem)
            {
                Owner = this
            };

            if (editWin.ShowDialog() == true)
            {
                var updatedItem = editWin.Result;

                try
                {
                    _viewModel.UpdateItem(updatedItem);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("编辑失败：" + ex.Message);
                }
            }
        }

        private void dgMaintenance_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if ((sender as DataGrid)?.SelectedItem is MaintenanceItemModel selectedItem)
            {
                var editWin = new wdAddMaintenance(selectedItem)
                {
                    Owner = this
                };

                if (editWin.ShowDialog() == true)
                {
                    var updatedItem = editWin.Result;
                    try
                    {
                        _viewModel.UpdateItem(updatedItem);
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show("编辑失败：" + ex.Message);
                    }
                }
            }
        }
    }
}
